'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate',
        'validate.en', 'datetimepicker', 'datetimepickerCN' ], function($) {
    return {
        init : function(data) {
           this.initInputResult();
           this.initSearch();
            this.initEdit(data.subCategory);
            this.initCreate(data.subCategory);
            this.initDelete();
            this.initCopy();
            this.initPhase();
            
        },
        initSearch: function() {
              
            $(document).on('shown.bs.modal', '#search', function(e) {
              $('[name="beginDateFrom"]').datetimepicker({
                format: 'yyyy-mm-dd',
                language: 'zh-CN',
                autoclose: 1,
                clearBtn:true,//清除按钮
                minView: 2,
                forceParse: 0
              });
              
              $('[name="beginDateTo"]').datetimepicker({
                  format: 'yyyy-mm-dd',
                  language: 'zh-CN',
                  autoclose: 1,
                  clearBtn:true,//清除按钮
                  minView: 2,
                  forceParse: 0
                });
              
              $('[name="endDateFrom"]').datetimepicker({
                  format: 'yyyy-mm-dd',
                  language: 'zh-CN',
                  autoclose: 1,
                  clearBtn:true,//清除按钮
                  minView: 2,
                  forceParse: 0
                });
            
            $('[name="endDateTo"]').datetimepicker({
                format: 'yyyy-mm-dd',
                language: 'zh-CN',
                autoclose: 1,
                clearBtn:true,//清除按钮
                minView: 2,
                forceParse: 0
              });
          });
            
        },
        
        initEdit : function(subCategory) {

            $(document).on('shown.bs.modal', '#edit', function(e) {
            	
            	var $eventSelect1 = $("select[name='firstCategoryId']");
            	$('select[name="firstCategoryId"]').select2({
      		      theme: "classic",
      		      placeholder : '请选择一级分类',
      		      minimumResultsForSearch : -1,
      		      allowClear : true
      		    });
            	
            	var fVal = $("select[name='firstCategoryId']").val()
            	var sVal = $("input[name='secondCategoryId']").data('text')
            	//console.log(sVal)
            	
            	$('input[name="secondCategoryId"]').select2({
        			theme: "classic",
      		        placeholder : '请选择二级分类',
      		        minimumResultsForSearch : 1,
      		        allowClear : true,
        			data:  subCategory[fVal]
        		}).val(sVal).trigger("change")
            	
            	//$('[name="secondCategoryId"]').prop("disabled", true);
            	
            	$eventSelect1.on("change" ,function(e) {
            		if(e.val) {
            			var item = subCategory[e.val]
                		$('input[name="secondCategoryId"]').prop("disabled", false);
                		$('input[name="secondCategoryId"]').val(null).trigger('change');
                		$('input[name="secondCategoryId"]').select2({
                			theme: "classic",
              		        placeholder : '请选择二级分类',
              		        minimumResultsForSearch : 1,
              		        allowClear : true,
                			data: item
                		})
            		} else {
            			$('input[name="secondCategoryId"]').select2("destroy");
            			$('input[name="secondCategoryId"]').val('');
            			$('[name="secondCategoryId"]').prop("disabled", true);
            		}
            		
            	})

               $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate
                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });
        },  
        initInputResult : function() {

            $(document).on('shown.bs.modal', '#inputResult', function(e) {
               $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate
                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });
        },         

         initPhase : function() {

            $(document).on('shown.bs.modal', '#phase', function(e) {

               $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate
              $('[name="beginDate"]').datetimepicker({
                  format: 'yyyy-mm-dd',
                  language: 'zh-CN',
                  autoclose: 1,
                  clearBtn:true,//清除按钮
                  minView: 2,
                  forceParse: 0
                });
                $('[name="endDate"]').datetimepicker({
                    format: 'yyyy-mm-dd',
                    language: 'zh-CN',
                    autoclose: 1,
                    clearBtn:true,//清除按钮
                    minView: 2,
                    forceParse: 0
                  });
                  
                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });
        },        
        initDelete : function() {

            $(document).on('shown.bs.modal', '#delete', function(e) {

                $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate

                var $form = $(document).find("form") // bind shiftform
                $form.shiftform({
                    beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                        return $.bt_validate.result[$.bt_validate.form_id]
                    }
                })
            });

        },
        
        initCopy : function() {

          $(document).on('shown.bs.modal', '#copy', function(e) {

              $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                              // validate

              var $form = $(document).find("form") // bind shiftform
              $form.shiftform({
                  beforeSubmit : function(el) {
                      $form.trigger("validate.ajax.submit");
                      return $.bt_validate.result[$.bt_validate.form_id]
                  }
              })
          });

      },

        
        initCreate : function(subCategory) {
        	
            $(document).on('shown.bs.modal', '#create', function(e) {
                
            	//var subCategory = data.subCategory;
            	var $eventSelect1 = $("select[name='firstCategory.id']");
            	$('select[name="firstCategory.id"]').select2({
      		      theme: "classic",
      		      placeholder : '请选择一级分类',
      		      minimumResultsForSearch : -1,
      		      allowClear : true
      		    });
            	
            	//$('[name="secondCategory.id"]').prop("disabled", true);
            	
            	$eventSelect1.on("change" ,function(e) {
            		if(e.val) {
            			var item = subCategory[e.val]
                		$('input[name="secondCategory.id"]').prop("disabled", false);
                		$('input[name="secondCategory.id"]').val(null).trigger('change');
                		$('input[name="secondCategory.id"]').select2({
                			theme: "classic",
              		        placeholder : '请选择二级分类',
              		        minimumResultsForSearch : 1,
              		        allowClear : true,
                			data: item
                		})
            		} else {
            			$('input[name="secondCategory.id"]').select2("destroy");
            			$('input[name="secondCategory.id"]').val('');
            			$('[name="secondCategory.id"]').prop("disabled", true);
            		}
            	})
                
                $(document).find('form[data-validate="true"]').bt_validate() // bind
                                                                                // validate
             var $form = $(document).find("form") // bind shiftform
              $form.shiftform({
                   beforeSubmit : function(el) {
                        $form.trigger("validate.ajax.submit");
                       return  $.bt_validate.result[$.bt_validate.form_id];
                        
                    }
               })
           });
        }
    }
})
